package com.mbf.crm.controller.websocket;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.mbf.crm.common.AjaxResult;
import com.mbf.crm.common.SocketMessage;
import com.mbf.crm.handler.WebSocketHandler;
import com.mbf.crm.model.domain.Member;

/**
 * <p><b>Copyright © 2010-2017 soho team All Rights Reserved<b></p>
 * IM控制器
 * @author 晏鼠  张劲松
 *
 */
@Controller
public class ImController {

	@Autowired
	private WebSocketHandler handler;
	
	/**
	 * 发送消息给指定用户
	 * @param uid
	 * @param content
	 * @param member
	 */
	@RequestMapping("/im/send")
	@ResponseBody
	public void send(Long uid,String content,@SessionAttribute("s_member") Member member){
		Map<String, Object> message = new HashMap<String, Object>();
		message.put("fromUid", member.getId());
		message.put("realName", member.getRealName());
		message.put("message", content);
		handler.sendMessageToUser(uid,  new SocketMessage("message", message).toTextMessage());
	}
	
	/**
	 * 获取在线用户列表
	 * @return
	 */
	@RequestMapping("/im/user/list")
	@ResponseBody
	public AjaxResult userList(){
		return new AjaxResult().setData(handler.getUsers());
	}
	
	
}
